package com.peanut.controller;

import com.mvc.annotation.*;
import com.peanut.entity.Game;
import com.peanut.entity.GameType;
import com.peanut.entity.JsonResult;
import com.peanut.entity.Page;
import com.peanut.service.GameService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Controller;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 游戏 和 游戏分类都在一起
 */

@Controller
public class GameController {
	@Autowired
	private GameService service;
	
	
	@RequestMapping("/gameType/query.do")
	@ResponseBody
	public JsonResult queryGameType(){
		List<GameType> gameTypes = service.queryGameType();
		return JsonResult.success(gameTypes);
	}
	
	
	// 注意：只能 status = 1 的信息 注意分页查询
	@RequestMapping("/game/queryNormal.do")
	@ResponseBody
	public JsonResult queryGame( @RequestParam(required = true, defaultValue = "1") Integer currentPage,
								 @RequestParam(required = true, defaultValue = "10") Integer pageSize){
		if(currentPage < 1){
			currentPage = 1;
		}
		if(pageSize < 1){
			pageSize = 10;
		}
		Page<Game> gamePage = service.queryNormalGame(currentPage, pageSize);
		return JsonResult.success(gamePage);
	}
	
	
	// 添加或者修改
	@RequestMapping("/game/update.do")
	@ResponseBody
	public JsonResult updateGame(@RequestBody @Valid Game game){
		boolean b = false;
		if(game.getId()==null){
			game.setCreateTime(LocalDateTime.now());
			b = service.save(game);
		}else{
			game.setUpdateTime(LocalDateTime.now());
			b = service.update(game);
		}
		if (b) {
			return JsonResult.success();
		}
		return JsonResult.fail();
	}
	
}
